package com.list88zhu.android.sunflower.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CalendarView;

import com.list88zhu.android.sunflower.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * <pre><code>
 *
 *  </code></pre>
 *
 * @author zhuyingming@qmtv.com
 * @date 2018/8/26
 */
public class CalendarDialog extends DialogFragment {


    private CalendarDialogCallback callback;

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());


    public void show(FragmentManager fragmentManager) {
        show(fragmentManager, "CalendarDialog");
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final CalendarView calendarView = (CalendarView) inflater.inflate(R.layout.calendar, null);
        calendarView.setOnDateChangeListener((calendarview, year, month, dayOfMonth) -> {
            String dates = year + "-" + ((month + 1) < 10 ? "0" + (month + 1) : (month + 1)) + "-" + (dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth);
            calendarview.setTag(dates);

        });
        builder.setView(calendarView);
        builder.setPositiveButton("确定", (dialog, which) -> {
                if (callback != null) {
                    String d = (String) calendarView.getTag();
                    if(TextUtils.isEmpty(d)) {
                        long value = calendarView.getDate();
                        Date date = new Date(value);
                        callback.confirm(format.format(date), date);
                    }else {
                        callback.confirm(d, string2date(d));
                    }

                }
            });
        return  builder.create();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        callback = null;
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    private Date string2date(String value) {
        try {
            Date date = format.parse(value);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String date2String(Date date) {
        String d = format.format(date);
        return d;
    }


    public CalendarDialogCallback getCallback() {
        return callback;
    }

    public void setCallback(CalendarDialogCallback callback) {
        this.callback = callback;
    }

    public interface CalendarDialogCallback {
        void confirm(String dateString, Date date);
    }
}
